package com.youme.section.shop

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.blankj.utilcode.util.ToastUtils
import com.hyphenate.easeim.R
import com.hyphenate.easeim.R.drawable.shape_jiesuan_bg
import com.hyphenate.easeim.R.drawable.shape_no_jiesuan_bg
import com.salton123.app.BaseApplication
import com.youme.model.FoodAdd
import com.youme.util.dp
import com.youme.util.stateRefresh
import com.youme.util.stateSave
import kotlinx.android.synthetic.main.layout_shop_price.view.*

class ShopPriceLayout : ConstraintLayout {

    private var num: Int = 0;

    private lateinit var mActivity: Activity

    private lateinit var carListenerr: ShowCarListenerr

    private var price = 0.0

    private var foods = 0

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    public fun setCarListener(carListenerr: ShowCarListenerr) {
        this.carListenerr = carListenerr
    }

    private fun animViews(): Array<View> = arrayOf(cl_main)

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_shop_price, this)
        im_car.setOnClickListener {
            carListenerr.show()
        }
        tv_lowest_price.setOnClickListener {
            if (num > 0) {
                carListenerr.jiesuan()

//                activity()?.startActivity(Intent(activity(),ChoujiangActivity().javaClass))
//                clean()
            } else {
                ToastUtils.showLong("请添加菜品！！！")
            }
        }
    }

    public fun setActivity(activity: Activity) {
        mActivity = activity
    }

    public fun clean() {
        tv_num.visibility = View.GONE
        tv_settlement_price.text = "0"
        tv_lowest_price.background = BaseApplication.sInstance.getDrawable(shape_no_jiesuan_bg)
    }

    public fun addCai(caiBean: FoodAdd) {
        foods = caiBean.productQuantity
        num++
        tv_num.visibility = View.VISIBLE
        tv_num.setText("" + foods)
        price = caiBean.productPrice * caiBean.productQuantity + price
        tv_settlement_price.text = price.toString()
        tv_lowest_price.background = BaseApplication.sInstance.getDrawable(shape_jiesuan_bg)
    }

    private var firstLayout: Boolean = false
    private var effected: Float = 0f

    override fun onWindowFocusChanged(hasWindowFocus: Boolean) {
        super.onWindowFocusChanged(hasWindowFocus)
        if (!firstLayout) {
            firstLayout = true
            //记录 View 状态
            cl_main.stateSave(R.id.viewStateStart).alpha(1F)
            cl_main.stateSave(R.id.viewStateEnd).alpha(0F)
        }
    }

    /**
     * 根据业务需求动态计算变化高度区别
     */
    fun effectByOffset(transY: Float) {
        effected = when {
            transY <= dp(140) -> 0F
            transY > dp(140) && transY < dp(230) -> (transY - dp(140)) / dp(90)
            else -> 1F
        }
        animViews().forEach { it.stateRefresh(R.id.viewStateStart, R.id.viewStateEnd, effected) }
    }

    private var mIsExpanded = false
}